package com.luyuan.crm.question.service.impl;

import java.util.List;

import org.hibernate.criterion.Order;

import com.luyuan.crm.member.model.Customer;
import com.luyuan.crm.question.model.Question;
import com.luyuan.crm.question.model.QuestionValue;
import com.luyuan.crm.question.service.QuestionValueService;
import com.ruijie.foundation.dao.support.HibernateEntityDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

@SuppressWarnings("unchecked")
public class QuestionValueServiceImpl extends HibernateEntityDao<QuestionValue> implements QuestionValueService {

    @Override
    public Page pagedQuery(CriteriaTemplate criteria, int pageNo, int pageSize, List<Order> orders) {
        CriteriaQueryTemplate cqt = new CriteriaQueryTemplate(getSessionFactory());
        return cqt.pagedQuery(criteria, pageNo, pageSize, orders);
    }

    @Override
    public void removeByQuestionId(int id) {
        this.createQuery("delete from QuestionValue q where q.question.id=?", id).executeUpdate();
    }

    @Override
    public List<QuestionValue> findByCustomer(Customer customer, Question question) {
        return find("select q from QuestionValue q left join q.questionExtend where q.customer=? and q.question=? order by q.questionExtend.sort asc", customer, question);
    }

    @Override
    public void saveQuestionValue(QuestionValue extend) {
        save(extend);
    }

    @Override
    public void updateQuestionValue(QuestionValue extend) {
        update(extend);
    }

    @Override
    public List<Question> getQuestionsByCustomer(Customer customer) {
        return find("select question from QuestionValue where customer = ? group by question", customer);
    }
}
